package com.lingo.ffmpegdemo.lg.iodemo;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.lingo.ffmpegdemo.lg.iodemo.util.ExcelUtils;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author watson
 * @date 2021/6/8 14:52
 * @Description: 读取文件，根据内容，写到对应的文件
 **/
public class Write2Excel {


    public static void main(String argv[]) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\data-kafka\\kafka\\");
        String[] list = file.list();

        for (int i = 0; i < list.length; i++) {
            getBuffer(list[i], i);
            System.out.println("succee" + i);
        }
        System.out.println("succeed!");

    }

    public static void getBuffer(String fileName, int i) {
        try {
            String fileNames = "C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\data-kafka\\kafka\\" + fileName;
//            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\hk_kafka-data\\" + fileName));
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileNames), "GBK"));
            String s = null;
            BufferedWriter bw = null;

            List<Map<String, String>> list1 = new ArrayList<>();
            List<Map<String, String>> list2 = new ArrayList<>();
            List<Map<String, String>> list3 = new ArrayList<>();
            List<Map<String, String>> list4 = new ArrayList<>();
            List<Map<String, String>> list5 = new ArrayList<>();
            List<Map<String, String>> list6 = new ArrayList<>();
            List<Map<String, String>> list7 = new ArrayList<>();
            List<Map<String, String>> list8 = new ArrayList<>();
            List<Map<String, String>> list9 = new ArrayList<>();
            List<Map<String, String>> list10 = new ArrayList<>();
            String name1 = "右线6号青岛入口";
            String name2 = "右线9号台三入口";
            String name3 = "右线10号黄岛出口";
            String name4 = "右线7号断面";
            String name5 = "右线8号海底";
            String name6 = "左线1号黄岛入口";
            String name7 = "左线4号团岛出口";
            String name8 = "左线5号青岛出口";
            String name9 = "左线3号断面";
            String name10 = "左线2号海底";

            while ((s = br.readLine()) != null) {
                int index = s.indexOf("为");
                if (index <= 0) {
                    continue;
                }
                String substring = s.substring(index + 1);
                Map map = null;
                try {
                    map = JSON.parseObject(substring, Map.class);
                } catch (Exception e) {
                    System.out.println(substring);
                    e.printStackTrace();
                }

                List<Map<String, Object>> mapList = (List<Map<String, Object>>) map.get("vehiclePassNoRecogList");
                Map<String, Object> stringObjectMap = mapList.get(0);
                //摄像头编码
                String cameraIndexCode = stringObjectMap.get("cameraIndexCode").toString();
                //拍摄时间
                String captureTime = stringObjectMap.get("captureTime").toString();
                //车牌
                String plateNo = stringObjectMap.get("plateNo").toString();
                //车道号
                String laneNo = stringObjectMap.get("laneNo").toString();

                LocalDateTime date = LocalDateTime.parse(captureTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
                captureTime = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));


                //右线6号青岛入口
                if (s.contains("0deff95a0d0942508af77a8b77568454")) {
//                    String name = "右线6号青岛入口";
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list1.add(mapl);
                    bw = getBufferedWriter(s, name1, bw);
                }
                //右线9号青岛入口(台三)
                if (s.contains("014eb55985ba4a5cb8ce6bbfb17856f6")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list2.add(mapl);
                    bw = getBufferedWriter(s, name2, bw);
                }
                //右线10号黄岛出口
                if (s.contains("c9e612c4562c48d0938c28e5863398d8")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list3.add(mapl);
                    bw = getBufferedWriter(s, name3, bw);
                }
                //右线断面
                if (s.contains("d8f3303a9cc645389d977897fb5a94c9")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list4.add(mapl);
                    bw = getBufferedWriter(s, name4, bw);
                }
                //右线海底
                if (s.contains("af8dc3626cfd4dd1a3b3dece8e29cb0f")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list5.add(mapl);
                    bw = getBufferedWriter(s, name5, bw);
                }


                //左线1号黄岛入口
                if (s.contains("5203f5da7a1f44318d8372e743b27eb0")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list6.add(mapl);
                    bw = getBufferedWriter(s, name6, bw);
                }
                //左线4号青岛出口(团岛)
                if (s.contains("c00437b9030c4487b98ac0d1b09ff34b")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list7.add(mapl);
                    bw = getBufferedWriter(s, name7, bw);
                }
                //左线5号青岛出口
                if (s.contains("20271af08fd24298a4cdc4845a29ed71")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list8.add(mapl);
                    bw = getBufferedWriter(s, name8, bw);
                }
                //左线断面
                if (s.contains("819af0cf44bc4b0186204e6735947554")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list9.add(mapl);
                    bw = getBufferedWriter(s, name9, bw);
                }
                //左线海底
                if (s.contains("41d5a9b92b714176b754276e8dccaf06")) {
                    Map<String, String> mapl = new HashMap<>();
                    mapl.put("cameraIndexCode", cameraIndexCode);
                    mapl.put("captureTime", captureTime);
                    mapl.put("plateNo", plateNo);
                    mapl.put("laneNo", laneNo);
                    list10.add(mapl);
                    bw = getBufferedWriter(s, name10, bw);
                }
            }

            if (i == 0) {
                ExcelUtils.writeExcel(list1, name1);
                ExcelUtils.writeExcel(list2, name2);
                ExcelUtils.writeExcel(list3, name3);
                ExcelUtils.writeExcel(list4, name4);
                ExcelUtils.writeExcel(list5, name5);
                ExcelUtils.writeExcel(list6, name6);
                ExcelUtils.writeExcel(list7, name7);
                ExcelUtils.writeExcel(list8, name8);
                ExcelUtils.writeExcel(list9, name9);
                ExcelUtils.writeExcel(list10, name10);
            } else {
                ExcelUtils.appendWriter(list1, name1);
                ExcelUtils.appendWriter(list2, name2);
                ExcelUtils.appendWriter(list3, name3);
                ExcelUtils.appendWriter(list4, name4);
                ExcelUtils.appendWriter(list5, name5);
                ExcelUtils.appendWriter(list6, name6);
                ExcelUtils.appendWriter(list7, name7);
                ExcelUtils.appendWriter(list8, name8);
                ExcelUtils.appendWriter(list9, name9);
                ExcelUtils.appendWriter(list10, name10);
            }

            bw.flush();
            if (bw != null) {
                bw.close();
            }
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getExcel(List<Map<String, String>> list1, String name) {
        String strPath = "C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + name + ".xlsx";
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(strPath);
        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(list1.size() - 1, name);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list1, true);
        // 关闭writer，释放内存
        writer.close();
    }

    private static BufferedWriter getBufferedWriter(String s, String a, BufferedWriter bw) throws IOException {
        if (bw != null) {
            //首先判断前一个文件是否为空，如不为空，可能在写前一个文件，则将其关闭。
            bw.close();
            //新建文件
            bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + a + ".txt", true));
        } else { //若为空，则是第一个文件
            bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + a + ".txt", true));
        }
        //将BC这一行内容写入
        bw.write(s + "\n");
        return bw;
    }
}
